//
// AWS Manager - A free AWS console application demonstrating advanced concepts
// Copyright (C) 2008-2010 Sean O'Dell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.awsmanager.utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 *
 * @author Sean ODell
 */
public class ProcessText {

    public ProcessText() {
    }

    public ArrayList processText(String aLine) throws Exception {
        ArrayList arrayList = new ArrayList();

        Scanner scanner = new Scanner(aLine);

        try {
            while (scanner.hasNextLine()) {
                try {
                    String string = scanner.nextLine();
                    if ((!string.equals(null)) && (!string.equals(""))) {
                        arrayList.add(string);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } finally {
            scanner.close();
        }
        return arrayList;
    }

    public ArrayList processText(String aLine, String aToken) throws Exception {
        ArrayList arrayList = new ArrayList();

        Scanner scanner = new Scanner(aLine);

        try {
            while (scanner.hasNextLine()) {
                try {
                    String string = scanner.nextLine();
                    if ((!string.equals(null)) && (!string.equals(""))) {

                        StringTokenizer stringparse = new StringTokenizer(string);

                        while (stringparse.hasMoreTokens()) {
                            arrayList.add(stringparse.nextToken(aToken).trim());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } finally {
            scanner.close();
        }
        return arrayList;
    }

    public boolean validCIDRIp(String cidrip, String type) {
        ArrayList cidrlist = new ArrayList();
        String ipAddress = null;
        String prefix = null;
        boolean isvalidIpAddr = true;
        boolean isvalidprefix = true;

        try {
            cidrlist = processText(cidrip, "/");
        } catch (Exception ex) {
            Logger.getLogger(ProcessText.class.getName()).log(Level.SEVERE, null, ex);
        }

        ipAddress = (String) cidrlist.get(0);
        isvalidIpAddr = validIpAddress(ipAddress);
        if (cidrlist.size() > 1) {
            prefix = (String) cidrlist.get(1);
            if ((Integer.parseInt(prefix) < 0) ||
                    (Integer.parseInt(prefix) > 32)) {
                isvalidprefix = false;
            }
        } else {
            if (type.equals("network")) {
                isvalidprefix = false;
            }
        }

        if (isvalidIpAddr && isvalidprefix) {
            return true;
        } else {
            return false;
        }
    }

    public boolean validIpAddress(String iPaddress) {
        String _255 = "(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
        Pattern IP_PATTERN = Pattern.compile("^(?:" + _255 + "\\.){3}" + _255 + "$");
        return IP_PATTERN.matcher(iPaddress).matches();
    }

    public String calToDateStr(Calendar calDate) {
        Date createDate = calDate.getTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
        return(dateFormat.format(createDate));
    }

    public InputStream strToInputStream(String str) {
        if (str == null) {
            return null;
        }
        str = str.trim();

        InputStream in = null;
        try {
            in = new ByteArrayInputStream(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ProcessText.class.getName()).log(Level.SEVERE, null, ex);
        }
        return in;
    }
}